﻿namespace Pottery.UnitTests.Scope.Store.UserController
{
    using System;
    using Pottery.Module.Infrastructure;
    using Pottery.Module.Store.Contracts;
    using Pottery.Module.Store;
    using Pottery.Module.Infrastructure.Interfaces;
    using Pottery.Module.Infrastructure.Models;
    using System.IO;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class Registrate : ITestUseCases
    {
        #region Constructors

        private Registrate()
        {
            m_Controller = m_provider.GetService(typeof(IUserContract)) as IUserContract;
            m_User = ((DomainControllerBase)m_Controller).Storage.Create<User>();
            m_Repository = new XmlRepository();
        }

        #endregion

        #region ITestUseCases Implementation

        public IPathResolver Resolver
        {
            set { m_path = value; }
        }

        public void SetInputData(bool IsValid)
        {
            string fileName = m_Resolver.ResolvePath(Path.Combine(typeof(User).Name, typeof(User).Name + "Valid.xml"));
            m_Repository.Initialize<User>(fileName);
            m_User = m_Repository.Item<User, int>(0);
            ((DomainControllerBase)m_Controller).Storage.Initialize<User>(Path.Combine(m_path.ResolvePath("UserController"), typeof(Registrate).Name,string.Concat(typeof(Registrate).Name, ".xml")));

            if (IsValid)
                m_User.Login = DateTime.Now.Ticks.ToString();

        }

        public bool AnalyseResults()
        {
            try
            {
                ((IUserContract)m_Controller).Registrate(m_User);

                return true;
            }
            catch { }

            return false;
        }

        #endregion

        #region Fields

        private IPathResolver m_path = new StorePathResolver();
        private IPathResolver m_Resolver = new DomainsPathResolver();
        private IServiceProvider m_provider = ServiceLocator.ServiceLocator.Instance;
        private IRepository m_Repository;
        private IUserContract m_Controller;
        private User m_User;

        #endregion
    }
}
